package com.rich.dmr.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.rich.dmr.R;

import java.util.List;

import rich.dmr.DmrManager;

public class GroupListGridAdapter extends RecyclerView.Adapter<GroupListGridAdapter.LinearViewHolder> {
    private String TAG = "rich---GroupListGridAdapter";
    private Context mContext;
    private OnItemClickListener mListener;
    private List<String> list;
    private DmrManager dmrManager;
    private int contact;


    @SuppressLint("WrongConstant")
    public GroupListGridAdapter(Context mContext, List<String> list) {
        this.mContext = mContext;
        this.list = list;
        dmrManager = (DmrManager) mContext.getSystemService("dmrservice");
        contact = dmrManager.getTransmitContact();
    }

    public interface OnItemClickListener {
        void ItemClickListener(View view, int position);

        void ItemLongClickListener(View view, int position);
    }

    public void setOnClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public GroupListGridAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_rv_group_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final GroupListGridAdapter.LinearViewHolder holder, int position) {
            String groupName = list.get(position);
            if (!groupName.equals("")) {
                holder.groupName.setText(groupName);
                if (contact == Integer.valueOf(list.get(position)) && dmrManager != null && dmrManager.getContactType() == 1) {
                    holder.groupOnline.setBackgroundResource(R.drawable.shape_online);
                }

                if (mListener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = holder.getLayoutPosition();
                            mListener.ItemClickListener(holder.itemView, pos);
                        }
                    });
                    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            int pos = holder.getLayoutPosition();
                            mListener.ItemLongClickListener(holder.itemView, pos);
                            return true;
                        }
                    });
                }
            }
    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    public class LinearViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout group;
        private TextView groupName;
        private TextView groupOnline;


        public LinearViewHolder(View itemView) {
            super(itemView);
            group = itemView.findViewById(R.id.grid_view);
            groupName = itemView.findViewById(R.id.group_name);
            groupOnline = itemView.findViewById(R.id.show_online);
        }
    }

}
